home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #14 / Monster Media No. 14 (April 1996) (Monster Media, Inc.).ISO / prog_d / oleauttr.zip / WORDDRIV.ZIP / WORDCLS.PAS next >
Pascal/Delphi Source File  |  1996-01-02  |  9KB  |  293 lines

  1. {
  2. // WORDCLS.PAS (C) 1995 W. Raike
  3. //              ALL RIGHTS RESERVED.
  4. //   NOTICE:    This file consists of trade secrets that are the property
  5. //              of William Raike.  The contents may not be used or disclosed
  6. //              without express written permission of the owner.
  7.  
  8. //   Revised 5/10/95 to include private FSelection member
  9. //   alloc/dealloc. in constructor/destructor, and Selection method.
  10.  
  11. //   Added to Ver 1.1 TOLEAutoClient distribution package - 31/12/95.
  12. }
  13. unit WordCls;
  14. {*******************************************************************}
  15. {
  16.   Class TWordObj that "mirrors" (some of) the properties and methods
  17.   exposed by the Microsoft Word for Windows 6.0 OLE Automation server.
  18.   This class models only a few of the hundreds of WordBasic commands,
  19.   for illustrative purposes.  For details of WordBasic syntax, refer
  20.   to the online help supplied with Microsoft Word and also to the file
  21.   POSITION.TXT supplied by Microsoft with the Microsoft Office Developers'
  22.   Kit (ODK).  This latter file correctly describes the order of the
  23.   arguments for WordBasic commands/macros, sometimes shown incorrectly
  24.   in the online help.
  25.  
  26.   Also works with Word 7.0 for Windows 95.
  27.   ***NOTE***  This implementation works only for English-language
  28.               versions of Word.  If your copy of Word is for a
  29.               different language, you will need to change the
  30.               strings containing names of the WordBasic commands
  31.               to correspond to those supported by your version of Word.
  32. }
  33. {*******************************************************************}
  34.  
  35. interface
  36.  
  37. uses
  38.     SysUtils, OleAuto;
  39.  
  40. type
  41.   TWordObj = class(TOleObject)
  42.   private
  43.     FSelection : PChar;
  44.   public
  45.     constructor CreateObject(progID : String); override;
  46.     destructor Release; override;
  47.  
  48.     procedure AllCaps;
  49.     procedure AppShow;
  50.     procedure Cancel;
  51.     function CountFoundFiles : Integer;
  52.     procedure EditSelectAll;
  53.     procedure FileClose(CloseMode : Integer);
  54.     procedure FileFind(Name, SearchPath : String);
  55.     procedure FileNew(TemplateName : String);
  56.     procedure FileOpen(FileName : String);
  57.     procedure FileSaveAs(FileName : String);
  58.     function FoundFileName(index : Integer) : String;
  59.     procedure Insert(pszText : PChar);
  60.     function Selection : PChar;
  61.     procedure SetTab(TabPos : String);
  62.     procedure ToolsMacro(MacroName : String);
  63.     procedure ToolsOptionsEdit;
  64.   end;
  65.  
  66. implementation
  67.  
  68. constructor TWordObj.CreateObject(progID : String);
  69. begin
  70.   { Override the usual CreateObject constructor in order to
  71.     allocate space to hold the text returned by the WordBasic
  72.     Selection$() function. }
  73.   inherited CreateObject(progID);
  74.   { The maximum length of the Selection$() string in WordBasic is 65280. }
  75.   try
  76.     FSelection := StrAlloc(65281);
  77.     FSelection[0] := #0;
  78.   except
  79.     FSelection := nil;  { Swallow exception if memory is unavailable. }
  80.   end;
  81. end;
  82.  
  83. destructor TWordObj.Release;
  84. begin
  85.   { Override destructor to deallocate memory allocated in constructor. }
  86.   if Assigned(FSelection) then
  87.     StrDispose(FSelection);
  88.   inherited Release;
  89. end;
  90.  
  91. procedure TWordObj.AllCaps;
  92. begin
  93.   CallOleProc('AllCaps');
  94. end;
  95.  
  96. procedure TWordObj.AppShow;
  97. begin
  98.   CallOleProc('AppShow');
  99. end;
  100.  
  101. procedure TWordObj.Cancel;
  102. begin
  103.   CallOleProc('Cancel');
  104. end;
  105.  
  106. function TWordObj.CountFoundFiles : Integer;
  107. var
  108.   Count : Integer;
  109. begin
  110.   CallOleFunction('CountFoundFiles', 'Integer', Count);
  111.   Result := Count;
  112. end;
  113.  
  114. procedure TWordObj.EditSelectAll;
  115. begin
  116.   CallOleProc('EditSelectAll');
  117. end;
  118.  
  119. procedure TWordObj.FileClose(CloseMode : Integer);
  120. begin
  121.   SetOleMethodArg('Integer', CloseMode);
  122.   CallOleProc('FileClose');
  123. end;
  124.  
  125. procedure TWordObj.FileFind(Name, SearchPath : String);
  126. const
  127.   pszEmpty : PChar = #0;
  128. var
  129.   nZero : Integer;
  130.   pszName, pszSearchPath : PChar;
  131. begin
  132.   pszName := StrAlloc(256);
  133.   pszSearchPath := StrAlloc(256);
  134.   StrPLCopy(pszName, Name, 255);
  135.   StrPLCopy(pszSearchPath, SearchPath, 255);
  136.   nZero := 0;
  137.  
  138.   SetOleMethodArg('Integer', nZero);  {.Delete}
  139.   SetOleMethodArg('Integer', nZero);  {.Add}
  140.   SetOleMethodArg('Integer', nZero);  {.SelectedFile}
  141.   SetOleMethodArg('Integer', nZero);  {.ListBy}
  142.   SetOleMethodArg('Integer', nZero);  {.SortBy}
  143.   SetOleMethodArg('Integer', nZero);  {.View}
  144.   SetOleMethodArg('PChar', pszEmpty);  {.DateCreatedTo}
  145.   SetOleMethodArg('PChar', pszEmpty);  {.DateCreatedFrom}
  146.   SetOleMethodArg('PChar', pszEmpty);  {.SavedBy}
  147.   SetOleMethodArg('PChar', pszEmpty);  {.DateSavedTo}
  148.   SetOleMethodArg('PChar', pszEmpty);  {.DateSavedFrom}
  149.   SetOleMethodArg('Integer', nZero);  {.PatternMatch}
  150.   SetOleMethodArg('PChar', pszEmpty);  {.Text}
  151.   SetOleMethodArg('Integer', nZero);  {.MatchCase}
  152.   SetOleMethodArg('Integer', nZero);  {.Options}
  153.   SetOleMethodArg('PChar', pszEmpty);  {.Subject}
  154.   SetOleMethodArg('PChar', pszEmpty);  {.Keywords}
  155.   SetOleMethodArg('PChar', pszEmpty);  {.Author}
  156.   SetOleMethodArg('PChar', pszEmpty);  {.Title}
  157.   SetOleMethodArg('Integer', nZero);  {.SubDir}
  158.   SetOleMethodArg('PChar', pszName);  {.Name}
  159.   SetOleMethodArg('PChar', pszSearchPath); {.SearchPath}
  160.   SetOleMethodArg('PChar', pszEmpty);  {.SearchName}
  161.  
  162.   CallOleProc('FileFind');
  163.  
  164.   StrDispose(pszName);
  165.   StrDispose(pszSearchPath);
  166. end;
  167.  
  168. procedure TWordObj.FileNew(TemplateName : String);
  169. var
  170.   pszName : PChar;
  171. begin
  172.   pszName := StrAlloc(256);
  173.   StrPLCopy(pszName, TemplateName, 255);
  174.   SetOleMethodArg('PChar', pszName);
  175.   CallOleProc('FileNew');
  176.   StrDispose(pszName);
  177. end;
  178.  
  179. procedure TWordObj.FileOpen(FileName : String);
  180. var
  181.   pszName : PChar;
  182. begin
  183.   pszName := StrAlloc(256);
  184.   StrPLCopy(pszName, FileName, 255);
  185.   SetOleMethodArg('PChar', pszName);
  186.   CallOleProc('FileOpen');
  187.   StrDispose(pszName);
  188. end;
  189.  
  190. procedure TWordObj.FileSaveAs(FileName : String);
  191. var
  192.   pszName : PChar;
  193. begin
  194.   pszName := StrAlloc(256);
  195.   StrPLCopy(pszName, FileName, 255);
  196.   SetOleMethodArg('PChar', pszName);
  197.   CallOleProc('FileSaveAs');
  198.   StrDispose(pszName);
  199. end;
  200.  
  201. function TWordObj.FoundFileName(index : Integer) : String;
  202. var
  203.   pszName : PChar;
  204. begin
  205.   if index <= 0 then Exit;
  206.   pszName := StrAlloc(256);
  207.   SetOleMethodArg('Integer', index);
  208.   CallOleFunction('FoundFileName$', 'PChar', pszName);
  209.   Result := StrPas(pszName);
  210.   StrDispose(pszName);
  211. end;
  212.  
  213. procedure TWordObj.Insert(pszText : PChar);
  214. begin
  215.   SetOleMethodArg('PChar', pszText);
  216.   CallOleProc('Insert');
  217. end;
  218.  
  219. function TWordObj.Selection : PChar;
  220. begin
  221.   if Assigned(FSelection) then
  222.   begin
  223.     CallOleFunction('Selection$', 'PChar', FSelection);
  224.     Result := FSelection;
  225.   end
  226.   else
  227.     Result := nil;
  228. end;
  229.  
  230. procedure TWordObj.SetTab(TabPos : String);
  231. const
  232.   pszPos : PChar = 'zzzzzzzzzz';
  233.   pszDefTabs : PChar = '1.0';
  234. var
  235.   n0, n1 : Integer;
  236. begin
  237.   n0 := 0;
  238.   n1 := 1;
  239.   StrPLCopy(pszPos, TabPos, 6);
  240.  
  241.   SetOleMethodArg('Integer', n0); { .ClearAll }
  242.   SetOleMethodArg('Integer', n0); { .Clear }
  243.   SetOleMethodArg('Integer', n1); { .Set }
  244.   SetOleMethodArg('Integer', n0); { .Leader }
  245.   SetOleMethodArg('Integer', n0); { .Align }
  246.   SetOleMethodArg('PChar', pszDefTabs);   { .DefTabs }
  247.   SetOleMethodArg('PChar', pszPos);   { .Position }
  248.   CallOLEProc('FormatTabs');
  249. end;
  250.  
  251. procedure TWordObj.ToolsMacro(MacroName : String);
  252. var
  253.   mode : Integer;
  254.   pszName : PChar;
  255. begin
  256.   mode := 1;
  257.   pszName := StrAlloc(256);
  258.   SetOleMethodArg('Integer', mode); { This arg *MUST* be present. }
  259.   StrPLCopy(pszName, MacroName, 255);
  260.   SetOleMethodArg('PChar', pszName);
  261.   CallOleProc('ToolsMacro');
  262.   StrDispose(pszName);
  263. end;
  264.  
  265. procedure TWordObj.ToolsOptionsEdit;
  266. const
  267.   pszMSW : PChar = 'Microsoft Word';
  268. var
  269.   x : Integer;
  270. begin
  271.   { It's necessary to supply ALL the arguments to ToolsOptionsEdit. }
  272.   {  We always set the arguments in reverse order: i.e., right to left. }
  273.   {
  274.       For this WordBasic command, all the options indicate check-box status
  275.       values (0 = unchecked) in the Word 6 Tools|Options\Edit dialog.
  276.   }
  277.   SetOleMethodArg('PChar', pszMSW); { .PictureEditor }
  278.  
  279.   x := 0; { Unchecked. }
  280.   SetOleMethodArg('Integer', x);  { .AllowAccentedUppercase }
  281.   SetOleMethodArg('Integer', x);  { .SmartCutPaste }
  282.   SetOleMethodArg('Integer', x);  { .Overtype }
  283.   SetOleMethodArg('Integer', x);  { .InsForPaste }
  284.   SetOleMethodArg('Integer', x);  { .AutoWordSelection }
  285.  
  286.   x := 1; { Checked. }
  287.   SetOleMethodArg('Integer', x);  { .DragAndDrop }
  288.   SetOleMethodArg('Integer', x);  { .ReplaceSelection }
  289.   CallOleProc('ToolsOptionsEdit');
  290. end;
  291.  
  292. end.
  293.